﻿@using OA.AspNetCore.Html;
@using OA.Model;
@using OA.Core.Model;
@using System.Text
@model List<Dictionary<string, string>>

@{
    Layout = null;
    string tableAttribute = ViewData["TableAttribute"].ToString();
    List<ExtendField> list = OA.Core.Helper.JsonHelper.DeserializeList<ExtendField>(ViewData["TableInfo"].ToString());
    list = list.Where(m => m.IsDraftShow).ToList();
    Queue<string> parentsName = new Queue<string>();
    int index = 0;
    Dictionary<string, int> dicIndex = new Dictionary<string, int>();
    Dictionary<string, int> dicColspan = new Dictionary<string, int>();
    foreach (var item in list)
    {
        if (!parentsName.Contains(item.ParentName))
        {
            parentsName.Enqueue(item.ParentName);
            item.Sort = index;
            dicIndex.Add(item.ParentName, index);
            dicColspan.Add(item.ParentName, 1);
        }
        else
        {
            item.Sort = dicIndex[item.ParentName];
            dicColspan[item.ParentName] += 1;
        }
    }
    list = list.OrderBy(m => m.Sort).ToList();
    string jsContent = string.Empty;
}

<input type="hidden" value="" name="dicExtend.@tableAttribute" id="dicExtend_@tableAttribute" />
<table class="layui-table mytablecontent" data-tableattribute="@tableAttribute" id="table_@tableAttribute">
    <thead>
        @if (list != null)
        {
            if (parentsName.Count != list.Count)
            {
        <tr>
            @foreach (var parentName in parentsName)
            {
                int children = dicColspan[parentName];
                if (children == 1)
                {
                    <td rowspan="2" style="text-align:center;">@parentName</td>
                }
                else
                {
                    <td colspan="@children" style="text-align:center;">@parentName</td>
                }
            }
            <td rowspan="2" style="text-align:center;">#</td>
        </tr>
            }
        }
    <tr>
        @foreach (var item in list)
        {
            if (parentsName.Count == list.Count || dicColspan[item.ParentName] > 1)
            {
                <th> @item.Name </th>
            }
        }
        @if (parentsName.Count == list.Count)
        {
            <td style="text-align:center;">#</td>
        }
    </tr>
    </thead>
    <tbody>
        @for (int i = 0; i < Model.Count; i++)
        {
            var valueItem = Model[i];

            <tr data-id="@i">
                @for (int j = 0; j < list.Count; j++)
                {
                    ExtendField infoExtendField = list[j];                    
                    var cellValue = string.Empty;
                    if (valueItem.ContainsKey(infoExtendField.Attribute))
                    {
                        infoExtendField.Value = valueItem[infoExtendField.Attribute];
                    }
                    else
                    {
                        infoExtendField.Value = infoExtendField.DefaultValue;  //tableValues.RowValues[0].CellValues.__F
                    }
                    <td class="item_@infoExtendField.Type" data-attribute="@infoExtendField.Attribute" data-type="@infoExtendField.Type" data-values="@Html.Raw(infoExtendField.Values)">
                        @Html.Raw(System.Text.RegularExpressions.Regex.Replace(MvcHtmlExtension.GetMyEditorControl(infoExtendField, tableAttribute + "[" + i.ToString() + "]."), "name=\".[^\"]+\"", ""))
                    </td>
                }
                <td>
                    <input type="hidden" class="Status" value="1" name="tableValues.RowValues[@i].Status" />
                    <span class="layui-btn layui-btn-xs layui-btn-danger deleteItem">删除</span>
                </td>
            </tr>
        }
    </tbody>
    <tfoot>
        @{
            int colspan = list.Count + 1;
        }
        <tr><td colspan="@colspan" class="right_content"><button type="button" class="layui-btn layui-btn-normal addItem">增加一行</button></td></tr>
    </tfoot>
</table>
<script type="text/javascript">
    tableRowTemplate.@tableAttribute= $("#table_@tableAttribute tbody tr").first().html();
</script>